package com.example.programexpert

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Spinner
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import android.widget.ScrollView

class MainActivity : ComponentActivity() {


    val expert = ProgramExpert(this)

    @SuppressLint("MissingInflatedId", "SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.mainlayout)

        val spinner = findViewById<Spinner>(R.id.spinner)
        val button = findViewById<Button>(R.id.button)
        val textView = findViewById<TextView>(R.id.textView)

        button.setOnClickListener {
            textView.text = expert.getLanguage(spinner.selectedItem.toString())
        }


        val dynamicTexts: LinearLayout = findViewById(R.id.dynamic_texts)
        val buttonAddText: Button = findViewById(R.id.button2)

        buttonAddText.setOnClickListener {
            val count = dynamicTexts.childCount + 1
            val dynamicText = TextView(this).apply {
                text = getString(R.string.content, count) + "\n"
                textSize = 16f
                setBackgroundColor(android.graphics.Color.rgb(150,255,255))
                setPadding(16,12,16,12)
            }
            dynamicTexts.addView(dynamicText)
            /* 自动滚到底 */
            findViewById<ScrollView>(R.id.scroll_container).post {
                findViewById<ScrollView>(R.id.scroll_container).fullScroll(ScrollView.FOCUS_DOWN)
            }
        }

    }
}
